package com.platform.hjt.sys.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hjt.tools.dao.Page;
import com.hjt.tools.dao.PropertyFilter;
import com.platform.hjt.base.controller.BaseController;
import com.platform.hjt.base.controller.ReturnBody;
import com.platform.hjt.base.costant.UserType;
import com.platform.hjt.sys.model.SysDept;
import com.platform.hjt.sys.service.SysDeptService;

@Controller
@RequestMapping(value="/sysDept")
public class SysDeptController extends BaseController{

	@Resource
	private SysDeptService sysDeptService;
	
	@RequestMapping(value="/sysDept_list.html")
	public String sysDeptListPage(){
		return "page/admin/sysDept/sysDept_list";
	}
	
	@RequestMapping(value="/sysDept_list.do")
	@ResponseBody
	public Page<SysDept> sysDeptListDo(Page<SysDept>page){
		List<PropertyFilter>  propertyFilterList = PropertyFilter.buildFromHttpRequest(getRequest());
		return sysDeptService.getSysDeptList(page, propertyFilterList);
	}
	
	@RequestMapping(value="/sysDept_edit.html")
	public String sysDeptEditPage(HttpServletRequest request, Model model, String id){
		if(StringUtils.isNotBlank(id)){
			model.addAttribute("sysDept", sysDeptService.getSysDept("sysDeptId", id));
		}
		
		return "page/admin/sysDept/sysDept_edit";
	}
	
	@RequestMapping(value="/sysDept_detail_{id}.html")
	public String sysDeptDetailPage(Model model, @PathVariable String id){
		model.addAttribute("sysDept", sysDeptService.getSysDept("String", id));
		return "page/admin/sysDept/sysDept_detail";
	}
	
	@RequestMapping(value="/sysDept_edit.do")
	@ResponseBody
	public ReturnBody sysDeptEditDo(@Valid @ModelAttribute("sysDept") SysDept sysDept, BindingResult result){
		if(result.hasErrors()){
			return new ReturnBody(false, result.getAllErrors().get(0).getDefaultMessage());
		}
		return sysDeptService.saveSysDept(sysDept);
	}
	
	@RequestMapping(value="/sysDept_del_{id}.do")
	@ResponseBody
	public ReturnBody sysDeptDelDo(@PathVariable String id){
		return sysDeptService.delSysDept(new SysDept(id));
	}
	
	@RequestMapping(value="/userCompanyDeptTree_{companyId}.do")
	@ResponseBody
	public JSONObject userCompanyDeptTree(@PathVariable String companyId){
		JSONObject json = new JSONObject();
		List<String> companyIds = new ArrayList<String>();
		
		if(!getSessionUser().getUserType().equals(UserType.MASTER.getKey())){
			companyIds.add(getSessionSysCompanyInfo().getSysCompanyInfoId());
		} else{
			companyIds.add(companyId);
		}
		
		List<SysDept> list  = sysDeptService.getSysDeptListByCompanyIds(companyIds);
		JSONArray arr = new JSONArray();
		for(SysDept sd : list){
			JSONObject temp = new JSONObject();
			temp.put("id", sd.getSysDeptId());
			temp.put("name", sd.getDeptName());
			temp.put("parentId", sd.getSysDept()!=null? sd.getSysDept().getSysDeptId(): "0");
			arr.add(temp);
		}
		json.put("treeNodes", arr);
		return json;
	}
	
	
}
